package edu.nf.service.impl;


import edu.nf.SQLExecutor;
import edu.nf.beans.annotations.Component;
import edu.nf.beans.annotations.Inject;
import edu.nf.dao.DeleteEmpDao;
import edu.nf.dao.FindEmpByIdDao;
import edu.nf.entity.Emp;
import edu.nf.exception.UpdateException;
import edu.nf.service.DeleteEmpService;
import edu.nf.utils.SQLExecutorFactory;

@Component("deleteEmpService")
public class DeleteEmpServiceImpl implements DeleteEmpService {

    @Inject(beanName = "findEmpById")
    private FindEmpByIdDao findEmpByIdDao;

    @Inject(beanName = "deleteEmpDao")
    private DeleteEmpDao deleteEmpDao;



    @Override
    public void deleteEmpById(String empid) {
        Emp emp = findEmpByIdDao.findEmpByEmpId(empid);
        String departmId = emp.getDepartmId();
        System.out.println(departmId);
        SQLExecutor se = SQLExecutorFactory.getSQLExecutor();
        se.beginTransaction();
        try{

            deleteEmpDao.deleteEmp(empid);
            se.commit();
        }catch (RuntimeException e){
            se.rollback();
            throw new UpdateException("delete fail.");
        }finally {
            SQLExecutorFactory.close();
        }
    }
}
